const { regeneratorRuntime, util, api } = global
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    mobile: '',
    carnumber: '',
    // dropcarnumber: '',
    canLogin: false,
    driverName: '',
    openInfo: {},
    showCar: '',
    showTrailer: '',
    token: ''
  },
  onLoad() {
    this.getOpenId()
  },
  // 登录
  async login(userInfo) {
    if (!this.data.mobile) {
      return util.showMsg('请输入手机号')
    }
    if (!this.data.carnumber) {
      return util.showMsg('请输入车牌号')
    }
    api.system.login({
      contactTel: this.data.mobile,
      plateNumber: this.data.carnumber,
      openId: this.data.openInfo.openid,
      driverName: this.data.driverName
    }).then((res) => {
      if (res.resultCode === 0) {
        util.showMsg('登录成功！')
        util.session('token', this.data.openInfo.openid)
        // let user = JSON.parse(util.session('user'))
        let user = {}
        if (userInfo) {
          user = Object.assign(userInfo, res.data)
        } else {
          user = res.data
        }
        util.session('user', JSON.stringify(user))
        app.globalData.token = util.session('token')
        setTimeout(() => {
          wx.switchTab({
            url: '/pages/index/index'
          })
        }, 500);
      } else {
        return util.showMsg(res.resultMsg)
      }
    }).catch((err) => {
      console.error(err)
      return util.showMsg(err)
    })
  },
  setCanLogin() {
    this.setData({
      canLogin: !!(this.data.mobile && this.data.carnumber && this.data.driverName)
    })
  },
  mobileChange(e) {
    this.setData({
      mobile: e.detail.value
    })
    this.setCanLogin()
  },
  carNumberChange(e) {
    this.setData({ 
      carnumber: e.detail.value
    })
    this.setCanLogin()
  },
  getPhoneNumber() {
    console.log('selMobile')
  },
  driverNameInput(e) {
    this.setData({
      driverName: e.detail.value
    })
    this.setCanLogin()
  },
  openCarPopup() {
    this.setData({
      showCar: true
    })
  },
  openTrailerPopup() {
    this.setData({
      showTrailer: true
    })
  },
  cancelCar() {
    this.setData({
      showCar: false
    })
  },
  onCarOk(e) {
    this.setData({
      carnumber: e.detail,
      showCar: false
    })
    this.setCanLogin()
  },
  cancelTrailer() {
    this.setData({
      showTrailer: false
    })
  },
  getUserInfo() {
    const that = this
    wx.getUserProfile({
      desc: '用于完善用户资料', 
      success: (res) => {
        if (res.errMsg === 'getUserProfile:ok') {
          that.login(res.userInfo)
        } else {
          util.showMsg('授权后才能登陆哦！')
        }
      }
    })
 },
  // 获取openId
  async getOpenId(user) {
    const res = await api.wxLogin()
    const ret = await api.user.getOpenId(res.code)
    if (ret.resultCode === 1) {
      util.showMsg('OpenId获取失败，请联系管理员！')
    }
    const userInfo = await api.user.judgeUser(ret.data.openid)
    if (userInfo.resultCode === 1) {
      util.showMsg(userInfo.resultMsg)
    }
    this.setData({
      openInfo: ret.data
    })
    util.session('openInfo', JSON.stringify(ret.data))
    if (userInfo.resultCode === 0 && userInfo.data) {
      // 已有用户
      app.globalData.token = ret.data.openid
      this.setData({
        driverName: userInfo.data.driverName,
        mobile: userInfo.data.contactTel,
        carnumber: userInfo.data.plateNumber,
        token: ret.data.openid,
        canLogin: true
      })
    } else {
      // util.session('user', JSON.stringify(user))
      // this.login()
    }
  },
});
